<?php

namespace App\Repositories\Album;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Album\AlbumGoodsCategory;
use Prettus\Repository\Traits\CacheableRepository;

/**
 * Class AlbumGoodsCategoryRepositoryEloquent.
 *
 * @package namespace App\Repositories\Album;
 */
class AlbumGoodsCategoryRepositoryEloquent extends BaseRepository implements AlbumGoodsCategoryRepository
{

    use CacheableRepository;

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AlbumGoodsCategory::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }


    /**
     * @author Jerry
     * @param int $albumId
     * @return mixed
     */
    public function findGoodsCategoryByAlbumId(int $albumId)
    {
        $where = [
            'template_id'   =>  $albumId,
        ];

        $expiresAt = Carbon::now()->addHour(1);

        return Cache::remember('album:categoryIds', $expiresAt, function () use ($where) {
            return $this->findWhere($where)->first()->toArray();
        });
    }

    /**
     * 查询缓存分类数据
     * @author Jerry
     * @param int $categoryId
     * @param int $goodsWhere
     * @param int $wid
     * @param int $isHidePromote
     * @return mixed
     */
    public function findCategoryGoodsCacheByCategoryId(int $categoryId, int $goodsWhere, int $wid = 8, int $isHidePromote = 0)
    {
        $expiresAt = Carbon::now()->addHour(1.2);

        return Cache::remember("album:categoryGoodsIds", $expiresAt, function () use ($isHidePromote, $wid, $goodsWhere, $categoryId) {
            return $this->findCategoryGoodsByCategoryId($categoryId, $goodsWhere, $wid, $isHidePromote);
        });
    }


    /**
     * @author Jerry
     * @param int $categoryId
     * @param int $goodsWhere
     * @param int $wid
     * @param int $isHidePromote
     * @return array
     */
    public function findCategoryGoodsByCategoryId(int $categoryId, int $goodsWhere, int $wid = 8, int $isHidePromote = 0)
    {
        $promoteTime = gm_time();

        $goods = [];

//        销量
        $categoryShowType = array(
            1	=>	20,
            2	=>	30,
            3	=>	50,
            16  =>  10,
        );

//		新品
        $categoryNewType = array(
            4	=>	20,
            5	=>	30,
            6	=>	50,
            17  =>  10,
        );

//		精品
        $categoryIsBest = array(
            7	=>	20,
            8	=>	30,
            9	=>	50,
            18  =>  10,
        );

//		热销
        $categoryIsHot = array(
            10	=>	20,
            11	=>	30,
            12	=>	50,
            19  =>  10,
        );

//		新品
        $categoryIsNew = array(
            13	=>	20,
            14	=>	30,
            15	=>	50,
            20  =>  10,
        );

        $where = false;

        $field = 'g.goods_id';

//        销量
        if (array_key_exists($goodsWhere, $categoryShowType)) {
            $where = true;

            $isBestWhere = [
                'g.cat_id'				=>	$categoryId,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
            ];


            if ($isHidePromote) {
                $isBestWhere[] = ['g.promote_end_date', '>', $promoteTime];
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin(\DB::raw('(select goods_id, count(goods_id) as num from sdx_comment_goods GROUP BY goods_id) as sdx_cg'), function ($join) {
                    $join->on('g.goods_id', '=', 'cg.goods_id');
                })
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->where($isBestWhere)
                ->orderBy('cg.num', 'desc')
                ->limit($categoryShowType[$goodsWhere])
                ->pluck($field)
                ->collapse()
                ->toArray();
        }

//		新添加
        if (array_key_exists($goodsWhere, $categoryNewType)) {
            $where = true;

            $isBestWhere = [
                'g.cat_id'				=>	$categoryId,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
            ];

            if ($isHidePromote) {
                $isBestWhere[] = ['g.promote_end_date', '>', $promoteTime];
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->orderBy('g.add_time', 'desc')
                ->limit($categoryNewType[$goodsWhere])
                ->where($isBestWhere)
                ->pluck($field)
                ->collapse()
                ->toArray();
        }

//		精品
        if (array_key_exists($goodsWhere, $categoryIsBest)) {
            $time = time();

            $isBestWhere = [
                'g.cat_id'				=>	$categoryId,
                'g.is_best'				=>	1,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
                ['gg.is_best_start_time', '<', $time],
                ['gg.is_best_start_time', '>', $time],
            ];

            if ($isHidePromote) {
                $isBestWhere[] = ['g.promote_end_date', '>', $promoteTime];
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->leftJoin('goods as gg', 'g.goods_id', '=', 'gg.goods_id')
                ->orderBy('g.add_time', 'desc')
                ->limit($categoryIsBest[$goodsWhere])
                ->where($isBestWhere)
                ->pluck($field)
                ->collapse()
                ->toArray();
        }

//		新品
        if (array_key_exists($goodsWhere, $categoryIsNew)) {
            $time = time();

            $isBestWhere = [
                'g.cat_id'				=>	$categoryId,
                'g.is_best'				=>	1,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
                ['gg.is_new_start_time', '<', $time],
                ['gg.is_new_start_time', '>', $time],
            ];

            if ($isHidePromote) {
                $isBestWhere[] = ['g.promote_end_date', '>', $promoteTime];
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->leftJoin('goods as gg', 'g.goods_id', '=', 'gg.goods_id')
                ->limit($categoryIsNew[$goodsWhere])
                ->where($isBestWhere)
                ->pluck($field)
                ->collapse()
                ->toArray();
        }

//		热销
        if (array_key_exists($goodsWhere, $categoryIsHot)) {
            $time = time();

            $isBestWhere = [
                'g.cat_id'				=>	$categoryId,
                'g.is_hot'				=>	1,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
                ['gg.is_hot_start_time', '<', $time],
                ['gg.is_hot_end_time', '>', $time],
            ];

            if ($isHidePromote) {
                $isBestWhere[] = ['g.promote_end_date', '>', $promoteTime];
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->leftJoin('goods as gg', 'g.goods_id', '=', 'gg.goods_id')
                ->limit($categoryIsHot[$goodsWhere])
                ->where($isBestWhere)
                ->pluck($field)
                ->collapse()
                ->toArray();
        }

        if ($where === false) {

            $isBestWhere = array(
                'g.cat_id'				=>	$categoryId,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                ['g.original_img', '<>', "''"],
            );

            if ($isHidePromote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

            $goods = \DB::table('goods_wid_' . $wid . ' as g')
                ->leftJoin('goods_storage as gs', function ($join) use ($wid) {
                    $join->on('g.goods_id', '=', 'gs.goods_id')->where('gs.wid', '=', $wid);
                })
                ->where($isBestWhere)
                ->pluck($field)
                ->groupBy('g.goods_id')
                ->collapse()
                ->toArray();
        }

        return $goods;

    }
    
}
